package com.morn.weibo.types;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.j256.ormlite.field.DatabaseField;
import com.morn.weibo.exception.WeiboException;
import com.morn.weibo.utils.DateUtils;

public class Status implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8771484019740709705L;

	@DatabaseField(id = true)
	private String id;

	@DatabaseField
	private String text;

	@DatabaseField
	private Date created_at;

	@DatabaseField
	private String source;

	@DatabaseField
	private boolean truncated;

	@DatabaseField
	private String in_reply_to_status_id;

	@DatabaseField
	private String in_reply_to_user_id;

	@DatabaseField
	private boolean favorited;

	@DatabaseField
	private String in_reply_to_screen_name;

	@DatabaseField(foreign = true)
	private User user;

	@DatabaseField(foreign = true)
	private Photo photo;

	public Status() {
	}

	public Status(JSONObject json) throws WeiboException {
		init(json);
	}

	private void init(JSONObject json) throws WeiboException {
		try {
			if (json != null) {
				created_at = DateUtils.parseDate(json.getString("created_at"),
						"EEE MMM dd HH:mm:ss ZZZZZ yyyy");
				;
				id = json.getString("id");
				text = json.getString("text");
				text = text.replaceAll("@<a href=\"http://fanfou.com/",
						"@<a href=\"FanfouUser://");
				text = text.replaceAll("#<a href=\"/q/",
						"#<a href=\"FanfouSearch://");
				source = json.getString("source");
				truncated = json.getBoolean("truncated");
				if (!json.isNull("in_reply_to_status_id")) {
					in_reply_to_status_id = json
							.getString("in_reply_to_status_id");
				}
				if (!json.isNull("in_reply_to_status_id")) {
					in_reply_to_user_id = json.getString("in_reply_to_user_id");
				}

				favorited = json.getBoolean("favorited");
				in_reply_to_screen_name = json
						.getString("in_reply_to_screen_name");
				if (!json.isNull("user")) {
					user = new User(json.getJSONObject("user"));
				}
				if (!json.isNull("photo")) {
					photo = new Photo(json.getJSONObject("photo"));
				}
			}
		} catch (JSONException e) {
			throw new WeiboException(e.getMessage() + ":" + json.toString(), e);
		}

	}

	public static List<Status> getStatusList(JSONArray array)
			throws JSONException, WeiboException {
		int size = array.length();
		List<Status> list = new ArrayList<Status>();
		for (int i = 0; i < size; i++) {
			list.add(new Status(array.getJSONObject(i)));
		}
		return list;
	}

	public Date getCreated_at() {
		return created_at;
	}

	public String getId() {
		return id;
	}

	public String getText() {
		return text;
	}

	public String getSource() {
		return source;
	}

	public boolean isTruncated() {
		return truncated;
	}

	public String getIn_reply_to_status_id() {
		return in_reply_to_status_id;
	}

	public String getIn_reply_to_user_id() {
		return in_reply_to_user_id;
	}

	public boolean isFavorited() {
		return favorited;
	}

	public String getIn_reply_to_screen_name() {
		return in_reply_to_screen_name;
	}

	public User getUser() {
		return user;
	}

	public Photo getPhoto() {
		return photo;
	}

	public void setCreated_at(Date created_at) {
		this.created_at = created_at;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setText(String text) {
		this.text = text;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public void setTruncated(boolean truncated) {
		this.truncated = truncated;
	}

	public void setIn_reply_to_status_id(String in_reply_to_status_id) {
		this.in_reply_to_status_id = in_reply_to_status_id;
	}

	public void setIn_reply_to_user_id(String in_reply_to_user_id) {
		this.in_reply_to_user_id = in_reply_to_user_id;
	}

	public void setFavorited(boolean favorited) {
		this.favorited = favorited;
	}

	public void setIn_reply_to_screen_name(String in_reply_to_screen_name) {
		this.in_reply_to_screen_name = in_reply_to_screen_name;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setPhoto(Photo photo) {
		this.photo = photo;
	}

	@Override
	public String toString() {
		return "Status [created_at=" + created_at + ", id=" + id + ", text="
				+ text + ", source=" + source + ", truncated=" + truncated
				+ ", in_reply_to_status_id=" + in_reply_to_status_id
				+ ", in_reply_to_user_id=" + in_reply_to_user_id
				+ ", favorited=" + favorited + ", in_reply_to_screen_name="
				+ in_reply_to_screen_name + ", user=" + user + ", photo="
				+ photo + "]";
	}

}
